package com.glsc.ngateway.opmanage.controller;

import com.alibaba.fastjson.JSON;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.other.FileCenterTranData;
import com.glsc.ngateway.common.base.exception.PlatformException;
import com.glsc.ngateway.opmanage.dto.YspCustomerDto;
import com.glsc.ngateway.opmanage.event.vo.ArchiveUserSynEvent;
import com.glsc.ngateway.opmanage.service.FileCenterTranDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 档案中心相关接口
 */
@RestController
@RequestMapping("/api/file/center")
@Slf4j
public class FileCenterController {

    @Resource
    private FileCenterTranDataService fileCenterTranDataService;
    @Resource
    private ApplicationContext applicationContext;

    /**
     * 衍生品传输客户信息接口,可重复导入
     *
     * @param customers
     * @return
     * @throws Exception
     */
    @PostMapping("/ysp/customer")
    public PlatformResponse<?> yspCustomer(@RequestBody List<YspCustomerDto> customers) {
        Assert.notEmpty(customers, "传输数据不可为空");
        if (customers.size() > 100000) {
            throw PlatformException.error("数据量过大");
        }

        List<FileCenterTranData> saveList = Lists.newArrayList();
        for (YspCustomerDto customer : customers) {
            try {
                if (StringUtils.isBlank(customer.getClientId())) {
                    log.info("衍生品传输数据导入失败,没有第三方系统id{}", JSON.toJSONString(customer));
                    continue;
                }
                saveList.add(FileCenterTranData.builder().dataType("derClient").id("derClient" + customer.getClientId())
                        .thirdSystemData(customer.getClientName()).thirdSystemId(customer.getClientId()).clientType(customer.getClientType()).
                                partyName(customer.getPartyName()).partyOrganCode(customer.getPartyOrganCode()).build());


                ArchiveUserSynEvent archiveUserSynEvent = new ArchiveUserSynEvent(customer);
                archiveUserSynEvent.setClientType("12");
                if (customer.getClientId().startsWith("CP")) {
                    archiveUserSynEvent.setClientType("10");
                }

                archiveUserSynEvent.setClientId(customer.getClientId());
                archiveUserSynEvent.setClientName(customer.getClientName());
                archiveUserSynEvent.setPartyName(customer.getPartyName());
                archiveUserSynEvent.setPartyOrganCode(customer.getPartyOrganCode());

                applicationContext.publishEvent(archiveUserSynEvent);
            } catch (Exception e) {
                log.error("衍生品传输客户信息接口异常", e);
            }

        }
        if (CollectionUtils.isNotEmpty(saveList)) {
            fileCenterTranDataService.saveAll(saveList);
        }

        return PlatformResponse.successData("客户列表导入成功");
    }


}
